package com.xja.house.controller;

import com.xja.house.entity.Users;
import com.xja.house.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("user")
@SessionAttributes("loginuser")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @RequestMapping(value = "reg",method = RequestMethod.POST)
    public  String reg(Users users,String repassword, Model model){
        try{
            if(users==null || repassword==null || "".equals(repassword)){
                model.addAttribute("msg","全是必填内容");
                return "reg";
            }
            if(!users.getPassword().equals(repassword)){
                model.addAttribute("msg","两次输入密码不同");
                return "reg";
            }
            int a = usersService.insert(users);

            return "redirect:/login.jsp";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }
    @RequestMapping("login")
    public String login(String name,String password,Model model){
        try {
            Users loginuser = usersService.selectByName(name);
            if (loginuser==null){
                model.addAttribute("msg","用户不存在");
                return "login";
            }
            if (!loginuser.getPassword().equals(password)){
                model.addAttribute("msg","密码不对");
                return "login";
            }
            model.addAttribute("loginuser",loginuser);
            return "redirect:/house/guanli.do";
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
